package com.liusaidh.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liusaidh
 * @date 2024/5/16 15:31
 * @description 拦截器
 */
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String state = request.getParameter("state");
        String requestURI = request.getRequestURI();
        String username = (String) request.getSession().getAttribute("username");
        if (requestURI.startsWith("/weibo/manager") && username == null){
            response.sendRedirect("/weibo/userLogin");
            return false;
        }
        if ((request.getRequestURI() + "?state=" + state).equals("/weibo/blog/getIndexBlog?state=1")){
            return true;
        }
        if (request.getSession().getAttribute("id") == null){
            response.sendRedirect("/weibo/userLogin");
            return false;
        }
        return true;
    }
}
